//SUMMARY:  This generates the two maps shown in Figure 2 of Ballard-Rosa, Jensen & Scheve (2021)

//Set directory
cd "C:\Users\cambr\Dropbox\US Authoritarianism\Paper\ISQ\ISQ replication materials\Data"
global output "C:\Users\cambr\Dropbox\US Authoritarianism\Paper\ISQ\ISQ replication materials\Output" 

capture log close
clear all
set more off


*************** Prepare maps data **************

* Load CZ shock data
use cty2010_czone_shock, clear

* Define CZ shock variables
g shock=d_czone_imp_exp_usch_1991_2007
xtile shock_median=shock, nq(2)
g shock_above_median=(shock_median==2)

* Get information about county diversity
merge m:1 countyid2010 using ethnicity  
keep if _merge==3
drop _merge

* Define and label CZ shock and diversity variables
g diverse=(share_white<0.7) & share_white!=.
bysort czone: egen diverse_share=mean(diverse)
bysort czone: egen diverse_cz=max(diverse)

g large_shock_diverse=(shock_above_median==1 & diverse_cz==1)

lab var shock_above_median "Indicator for above-median shock exposure"
lab def largeshock 0 "Low exposure" 1 "High exposure" 
lab val shock_above_median largeshock

lab var diverse_cz "Indicator for at least one diverse county in the CZ"
lab def diverse 0 "Not diverse" 1 "Diverse" 
lab val diverse_cz diverse

lab var large_shock_diverse "Indicator for high shcok exposure and at least one diverse county in the CZ"
lab def shockdiverse 0 "Low exposure and/or not diverse" 1 "High exposure and diverse" 
lab val large_shock_diverse shockdiverse

keep czone shock shock_above_median diverse_share diverse_cz large_shock_diverse l_shind_manuf_cbp

* Keep unique CZs
duplicates drop czone, force
rename czone cz

* Merge onto maps data (coordinates)
merge 1:m cz using cz1990db
keep if _merge==3
drop _merge

* Define variable formats for maps
foreach x of varlist shock diverse_share  {
format `x' %9.2f
}

foreach x of varlist shock_above_median diverse_cz large_shock_diverse {
format `x' %9.0f
}




*************** Map graphs **************

*** Map showing quartiles of the shock

* Define quartile cutoffs to use in maps 
bysort cz: g count=_n
sort count
pctile cutoff_shock=shock if count==1, nq(4)
tab cutoff_shock

* Add min and max 
sum shock
replace cutoff_shock=r(min) if _n==4
replace cutoff_shock=r(max) if _n==5

tab cutoff_shock

levelsof cutoff_shock, local(levels)
global cutoff_shock "`levels'"
di $cutoff_shock

* Generate map graph
graph set window fontface "helvetica"
spmap shock using cz1990coord, id(id) fcolor("255 242 130" "255 201 0" "242 103 24" "155 0 0") ///
clmethod(custom) clbreaks($cutoff_shock) osize(vthin) ///
ocolor(white ..) ndocolor(white ..) ndfcolor(gs14) ///
title(/*"Commuting Zone Shock Exposure"*/"", size(medium) margin(t+4)) ///
/*subtitle("Indicator for above-median shock exposure", size(small))*/ ///
legend(label(5 "Q4")) ///
legend(label(4 "Q3")) ///
legend(label(3 "Q2")) ///
legend(label(2 "Q1")) ///
legend(region(fcolor(white) lcolor(white)) cols(6) position(6) colgap(*0.5)) ///
graphregion(color(white)) plotregion(margin(b+12 t+2)) 
graph export "$output/shock_intensity_CZ_q4.pdf", replace
drop count cutoff_shock


*** Map showing quartiles of the shock, conditional on initial local manufacturing

* Shock controlled for manufacturing
reg shock l_shind_manuf_cbp
g manu_effect=_b[l_shind_manuf_cbp]
g cz_manu_effect=l_shind_manuf_cbp*manu_effect
g shock_adjusted=shock-cz_manu_effect

format shock_adjusted %9.2f

* Define quartile cutoffs to use in maps 
bysort cz: g count=_n
sort count
pctile cutoff_shock_adjusted=shock_adjusted if count==1, nq(4)
tab cutoff_shock_adjusted

* Add min and max 
sum shock_adjusted
replace cutoff_shock_adjusted=r(min) if _n==4
replace cutoff_shock_adjusted=r(max) if _n==5

tab cutoff_shock_adjusted

levelsof cutoff_shock_adjusted, local(levels)
global cutoff_shock_adjusted "`levels'"
di $cutoff_shock_adjusted

* Generate map graph
graph set window fontface "helvetica"
spmap shock_adjusted using cz1990coord, id(id) fcolor("255 242 130" "255 201 0" "242 103 24" "155 0 0") ///
clmethod(custom) clbreaks($cutoff_shock_adjusted) osize(vthin) ///
ocolor(white ..) ndocolor(white ..) ndfcolor(gs14) ///
title(/*"Commuting Zone Shock Exposure"*/"", size(medium) margin(t+4)) ///
/*subtitle("Indicator for above-median shock exposure", size(small))*/ ///
legend(label(5 "Q4")) ///
legend(label(4 "Q3")) ///
legend(label(3 "Q2")) ///
legend(label(2 "Q1")) ///
legend(region(fcolor(white) lcolor(white)) cols(6) position(6) colgap(*0.5)) ///
graphregion(color(white)) plotregion(margin(b+12 t+2)) 
graph export "$output/shock_intensity_CZ_cond_manu_q4.pdf", replace
drop count cutoff_shock
